﻿// --------------------------------
// <copyright file="LevelBuilderModel.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>7. 12. 2011 18:54:00</date>

// <summary>
// 
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FITClub.Episode1;
using Microsoft.Xna.Framework.Graphics;
using FITClub.Episode1.Primitives3D;

namespace FITClub.LevelBuilder
{

        public class LevelBuilderModel
        {
            public GameFITClub game;
            public Model model;
            Matrix[] transforms;
            public Vector3 position, scale;
            public Matrix worldMatrix;
            public Vector3 color;
            public Texture2D Texture;
            public int SpriteSize = 20;
            public GeometricPrimitive PrimitiveObject;
            public bool PrimaryDraw = false;
            public float Alpha { get; set; }

            public LevelBuilderModel(GameFITClub game, Model model, Vector3 color, float alpha=1, bool sphere=true)

            {
                this.Alpha = alpha;
                this.game = game;
                this.model = model;                                
                this.transforms = new Matrix[this.model.Bones.Count];
                this.model.CopyAbsoluteBoneTransformsTo(this.transforms);
                this.color = color;
                if (sphere)
                {
                    this.PrimitiveObject = new SpherePrimitive(this.game.GraphicsDevice);
                }
                else
                {
                    this.PrimitiveObject = new CubePrimitive(this.game.GraphicsDevice);
                }
            }

            public LevelBuilderModel(GameFITClub game, Model model, Vector3 color, Texture2D texture, int size, bool sphere = true)
            {
                this.game = game;
                this.model = model;
                this.transforms = new Matrix[this.model.Bones.Count];
                this.model.CopyAbsoluteBoneTransformsTo(this.transforms);
                this.color = color;
                this.Texture = texture;
                this.SpriteSize = size;
                if (sphere)
                {
                    this.PrimitiveObject = new SpherePrimitive(this.game.GraphicsDevice);
                }
                else
                {
                    this.PrimitiveObject = new CubePrimitive(this.game.GraphicsDevice);
                }
            }


            /// <summary>
            /// Allows the game component to update itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            public  void Update(Vector2 pos, float radius)
            {
                Update( new Vector3(pos, 0.0f), radius);                
            }

            public void Update(Vector3 position, float radius)
            {
                this.position = position;
                scale = new Vector3(radius * 2.0f, radius * 2.0f, radius * 2.0f);
                worldMatrix = Matrix.CreateScale(scale) * Matrix.CreateTranslation(position);
            }

            /// <summary>
            /// Allows the game component to draw itself.
            /// </summary>
            public  void Draw()
            {
                if (this.PrimaryDraw && this.model != null)
                {
                    foreach (ModelMesh mesh in model.Meshes)
                    {
                        foreach (BasicEffect effect in mesh.Effects)
                        {
                            effect.Alpha = this.Alpha;
                           // effect.TextureEnabled = false;
                            //effect.DiffuseColor =  color;
                            effect.EnableDefaultLighting();
                            //effect.PreferPerPixelLighting = true;
                            effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                            effect.View = game.ViewMatrix;
                            effect.Projection = game.ProjectionMatrix;
                        }
                        mesh.Draw();
                    }
                }
                else
                {
                    this.PrimitiveObject.Draw(this.worldMatrix, this.game.ViewMatrix, this.game.ProjectionMatrix, new Color(this.color));
                }
            }
    }
}
